//
//  BaseCustomTablView.m
//  DDHuilai
//
//  Created by 宋华 on 16/7/22.
//  Copyright © 2016年 Admin. All rights reserved.
//

#import "BaseCustomTablView.h"

@implementation BaseCustomTablView

@synthesize hOrF;

#pragma mark - Cycle Life

// 初始化 TableView
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
    }
    return self;
}

- (id)init
{
    self = [super init];
    if (self) {

    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {

    }
    return self;
}

- (id )initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
}


#pragma mark - setter

- (void)setHOrF:(HaveHeadOrFooter)hf {
    hOrF = hf;
    if (hOrF == HaveHeadOrFooter_all) {
        [self addRefreshView];
    } else if (hOrF == HaveHeadOrFooter_head) {
        [self addHeadRefreshView];
    } else if (hOrF == HaveHeadOrFooter_footer) {
        [self addFooterView];
    }
}


#pragma mark -界面
- (void)addHeadRefreshView {
    __weak BaseCustomTablView *this = self;
    
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        if (self.refreshDelegate && [self.refreshDelegate respondsToSelector:@selector(loadedData:withLoadType:)]) {
            [this.refreshDelegate loadedData:this withLoadType:MYLoadDataTypeRefreshOne];
        }
        
        // 设置自动切换透明度(在导航栏下面自动隐藏)
        this.mj_header.automaticallyChangeAlpha = YES;
        
        // 隐藏时间
        [(MJRefreshNormalHeader *)this.mj_header lastUpdatedTimeLabel].hidden = YES;
    }];
}


- (void)addFooterView {
    __weak BaseCustomTablView *this = self;
    
    self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.refreshDelegate && [self.refreshDelegate respondsToSelector:@selector(loadedData:withLoadType:)]) {
            [this.refreshDelegate loadedData:this withLoadType:MYLoadDataTypeLoadMoreOne];
        }
        
        // 隐藏上拉文字
        ((MJRefreshAutoNormalFooter *)this.mj_footer).automaticallyHidden = YES;
    }];
    
    self.mj_footer.hidden = YES;
}


// 下拉刷新及上拉加载更多
- (void)addRefreshView
{
    [self addFooterView];
    [self addHeadRefreshView];
}

#pragma mark - Private Method

/**
 @brief 设置TableView表示没有更多数据
 */
- (void)noMoreData
{
    [self.mj_footer endRefreshingWithNoMoreData];// .state = MJRefreshFooterStateNoMoreData;
}

/**
 @brief 重置TableView表示没有更多数据
 */
- (void)resetNoMoreData
{
    if (self.mj_footer.state==MJRefreshStateNoMoreData) {
        [self.mj_footer resetNoMoreData];
    }
}

/**
 @brief 设置TableView进入画面自动刷新数据
 */
- (void)autoRefreshData
{
    [self.mj_header beginRefreshing];
}

/**
 @brief 停止刷新数据
 */
- (void)endRefreshing
{
    if ([self.mj_header isRefreshing]) {
        [self.mj_header endRefreshing];
    }
    
    if ([self.mj_footer isRefreshing]) {
        [self.mj_footer endRefreshing];
    }
    
    if ((int)self.contentSize.height == 0) {
        self.mj_footer.hidden = YES;
    }
    else
    {
        self.mj_footer.hidden = NO;
    }
}

@end
